/*
 * Copyright (C) 2008 Holger Hans Peter Freyther <zecke@selfish.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "GeolocationClientCd.h"

#if ENABLE(GEOLOCATION)
#include "WebView.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "Geolocation.h"
#include "GeolocationController.h"
#include "GeolocationError.h"
#include "GeolocationPosition.h"
#include "WebKitLogging.h"
#include "wtf/CurrentTime.h"
#include "Timer.h"
//#include "webkitgeolocationpolicydecisionprivate.h"
//#include "webkitwebframeprivate.h"
//#include "webkitwebviewprivate.h"

namespace WebKit {
class GPSTimer:public WebCore::TimerBase{
private:	
    WebView*view;
public:
    GPSTimer(WebView*v){
	   view=v;
    }
    void fired()override{
	  auto m_lastPosition = WebCore::GeolocationPosition::create(WTF::currentTime(),
                    .0/*latitude*/, .0/*longitude*/, 0/*accuracy*/, true, .0/*altitude*/, true, 0/*altitudeAccuracy*/, false, 0, false, 0);
          WebCore::GeolocationController::from(view->page())->positionChanged(m_lastPosition.get());
	  LOG(WebView,"...positionChanged...");
    }
};
static GPSTimer* gpsTimer;

GeolocationClientCd::GeolocationClientCd(WebView* webView)
   : m_webView(webView)//, m_provider(this)
{
    gpsTimer = new GPSTimer(webView);
    LOG(WebView,"====GeolocationClientCd::GeolocationClientCd====");
}

void GeolocationClientCd::geolocationDestroyed()
{
    delete this;
}

void GeolocationClientCd::startUpdating()
{
    gpsTimer->startRepeating(100);
    //m_provider.startUpdating();
}

void GeolocationClientCd::stopUpdating()
{
    gpsTimer->stop();
    //m_provider.stopUpdating();
}

void GeolocationClientCd::setEnableHighAccuracy(bool enable)
{
    //m_provider.setEnableHighAccuracy(enable);
}

WebCore::GeolocationPosition* GeolocationClientCd::lastPosition()
{
    return m_lastPosition.get();
}

void GeolocationClientCd::requestPermission(WebCore::Geolocation* geolocation)
{
    //WebKitWebFrame* webFrame = kit(geolocation->frame());
    //webkit_geolocation_policy_deny(policyDecision.get());
}

void GeolocationClientCd::cancelPermissionRequest(WebCore::Geolocation* geolocation)
{
}

void GeolocationClientCd::notifyPositionChanged(int timestamp, double latitude, double longitude, double altitude, double accuracy, double altitudeAccuracy)
{
    m_lastPosition = WebCore::GeolocationPosition::create(static_cast<double>(timestamp), 
		    latitude, longitude, accuracy, true, altitude, true, altitudeAccuracy, false, 0, false, 0);
    WebCore::GeolocationController::from(m_webView->page())->positionChanged(m_lastPosition.get());
    LOG(WebView,"GeolocationClientCd::notifyPositionChanged");
}

void GeolocationClientCd::notifyErrorOccurred(const char* message)
{
    RefPtr<WebCore::GeolocationError> error = WebCore::GeolocationError::create(WebCore::GeolocationError::PositionUnavailable, message);
    WebCore::GeolocationController::from(m_webView->page())->errorOccurred(error.get());
    LOG(WebView,"GeolocationClientCd::notifyErrorOccurred");
}

}

#endif // ENABLE(GEOLOCATION)
